package com.ruoyi.web.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.*;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ERP.BomInfoErp;
import com.ruoyi.system.service.IBomInfoErpService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * erp工单bom清单详情Controller
 *
 * @author 江小熊
 * @date 2024-09-30
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/base/bomInfoErp" )
public class BomInfoErpController extends BaseController {

    private final IBomInfoErpService iBomInfoErpService;

    /**
     * 查询erp工单bom清单详情列表
     */
//    @PreAuthorize("@ss.hasPermi('base:bomInfoErp:list')")
    @GetMapping("/list")
    public TableDataInfo list(BomInfoErp bomInfoErp)
    {
        startPage();
        LambdaQueryWrapper<BomInfoErp> lqw = new LambdaQueryWrapper<BomInfoErp>();
        if (StringUtils.isNotBlank(bomInfoErp.getScddErpId())){
            lqw.eq(BomInfoErp::getScddErpId ,bomInfoErp.getScddErpId());
        }
        if (StringUtils.isNotBlank(bomInfoErp.getChildId())){
            lqw.eq(BomInfoErp::getChildId ,bomInfoErp.getChildId());
        }
        if (StringUtils.isNotBlank(bomInfoErp.getChildCode())){
            lqw.eq(BomInfoErp::getChildCode ,bomInfoErp.getChildCode());
        }
        if (StringUtils.isNotBlank(bomInfoErp.getChildName())){
            lqw.like(BomInfoErp::getChildName ,bomInfoErp.getChildName());
        }
        if (StringUtils.isNotBlank(bomInfoErp.getChildNorms())){
            lqw.eq(BomInfoErp::getChildNorms ,bomInfoErp.getChildNorms());
        }
        if (StringUtils.isNotBlank(bomInfoErp.getChildAttribute())){
            lqw.eq(BomInfoErp::getChildAttribute ,bomInfoErp.getChildAttribute());
        }
        if (StringUtils.isNotBlank(bomInfoErp.getUnit())){
            lqw.eq(BomInfoErp::getUnit ,bomInfoErp.getUnit());
        }
        if (bomInfoErp.getBasicDosage() != null){
            lqw.eq(BomInfoErp::getBasicDosage ,bomInfoErp.getBasicDosage());
        }
        if (bomInfoErp.getBaseQuantity() != null){
            lqw.eq(BomInfoErp::getBaseQuantity ,bomInfoErp.getBaseQuantity());
        }
        List<BomInfoErp> list = iBomInfoErpService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出erp工单bom清单详情列表
     */
    @PreAuthorize("@ss.hasPermi('base:bomInfoErp:export')" )
    @Log(title = "erp工单bom清单详情" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(BomInfoErp bomInfoErp) {
        LambdaQueryWrapper<BomInfoErp> lqw = new LambdaQueryWrapper<BomInfoErp>(bomInfoErp);
        List<BomInfoErp> list = iBomInfoErpService.list(lqw);
        ExcelUtil<BomInfoErp> util = new ExcelUtil<BomInfoErp>(BomInfoErp. class);
        return util.exportExcel(list, "bomInfoErp" );
    }

    /**
     * 导入
     */
//    @PreAuthorize("@ss.hasPermi('base:bomInfoErp:import')" )
    @Log(title = "BOM维护导入" , businessType = BusinessType.IMPORT)
    @PostMapping("/importData" )
    public AjaxResult importData(@ApiParam(value = "导入文件", required = true) MultipartFile file,
                                 boolean updateSupport,String scddErpId,String taskTon) throws Exception {
        ExcelUtil<BomInfoErp> util = new ExcelUtil<BomInfoErp>(BomInfoErp.class);
        List<BomInfoErp> bomList = util.importExcel(file.getInputStream());

        for (int i = 0; i < bomList.size(); i++) {
            bomList.get(i).setChildId(String.valueOf(new Date().getTime()+i));
        }

        String operName = SecurityUtils.getUsername();
        String message = iBomInfoErpService.importBom(bomList, updateSupport, operName,scddErpId,taskTon);
        return AjaxResult.success(message);
    }

    /**
     * 导入模板
     */
    @ApiOperation(value = "导入模板")
    @GetMapping("/import/Template")
    public AjaxResult importTemplate()
    {
        List<BomInfoErp> list = new ArrayList<>();
        ExcelUtil<BomInfoErp> util = new ExcelUtil<BomInfoErp>(BomInfoErp.class);
        return util.importTemplateExcel(list,"BOM维护数据");
    }

    /**
     * 获取erp工单bom清单详情详细信息
     */
//    @PreAuthorize("@ss.hasPermi('base:bomInfoErp:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iBomInfoErpService.getById(id));
    }

    /**
     * 新增erp工单bom清单详情
     */
//    @PreAuthorize("@ss.hasPermi('base:bomInfoErp:add')" )
    @Log(title = "erp工单bom清单详情" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BomInfoErp bomInfoErp) {
        bomInfoErp.setChildId(String.valueOf(new Date().getTime()));
        return toAjax(iBomInfoErpService.save(bomInfoErp) ? 1 : 0);
    }

    /**
     * 修改erp工单bom清单详情
     */
//    @PreAuthorize("@ss.hasPermi('base:bomInfoErp:edit')" )
    @Log(title = "erp工单bom清单详情" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BomInfoErp bomInfoErp) {
        return toAjax(iBomInfoErpService.updateById(bomInfoErp) ? 1 : 0);
    }

    /**
     * 删除erp工单bom清单详情
     */
//    @PreAuthorize("@ss.hasPermi('base:bomInfoErp:remove')" )
    @Log(title = "erp工单bom清单详情" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iBomInfoErpService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
